<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Front as AM;
use app\admin\model\Mould;
use app\admin\model\SysType;
use app\admin\validate\FrontValidate;

class Front extends Common
{
    private $noCheckAction = ['entrance'];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
        $this->doCheckAuth($this->noCheckAction);
    }
    
    /**
    * 权限列表
    */
    public function index()
    {
        $model = new Am();
        $request = $this->request->get();
        $search_where = $model->_where($request);
        $where = $search_where['where'];
        $search = $search_where['search'];
        $data = $model->_list($where);
        //var_dump($data['list']);die;
        $count = $model->_count($where);
        $this->assign([
            'search' => $search,
            'list' => $data['list'],
            'page' => $data['page'],
            'count' => $count,
        ]);
        
        return $this->fetch();
    }

    /**
    * 添加
    */
    public function add(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            $pid = (int)$request['pid'];
            $validate = new FrontValidate();
            if(!$validate->scene($validate::SCENE_ADD)->check($request)){
                die(outputError($validate->getError()));
            }
            
                        
            $model = new AM();
           
            $ret = $model->_create($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '添加成功'));
        }else{
            $model = new AM();
            $list = $model->_getFrontAll();
            
            $mode1 = new SysType();
            $list1 = $mode1->_getAll();
            
            $model2 = new Mould();
            $list2 = $model2->_getAll();
            $this->assign([
                'list' => $list,
                'list1' => $list1,
                'list2' => $list2,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 编辑
    */
    public function edit(){
        if($this->request->isAjax()){
            $request = $this->request->post();
            //var_dump($request);die;
            $pid = (int)$request['pid'];
            if($pid != 0){
                $validate = new FrontValidate();
                if(!$validate->scene('edit_con')->check($request)){
                    die(outputError($validate->getError()));
                }
            }else{
                $validate = new FrontValidate();
                if(!$validate->scene($validate::SCENE_MODIFY)->check($request)){
                    die(outputError($validate->getError()));
                }
            }
            $model = new AM();
            $ret = $model->_modify($request);
            if(!$ret){
                die(outputError($model->getError()));
            }
            die(outputSuccess([], '编辑成功'));
        }else{
            $id = (int)$this->request->get('id');
            $model = new AM();
            $list = $model->_getFrontAll();
            
            $mode1 = new SysType();
            $list1 = $mode1->_getAll();
            
            $model2 = new Mould();
            $list2 = $model2->_getAll();
            $data = $model->_findOne($id);
            $this->assign([
                'data'=>$data, 
                'list' => $list,
                'list1' => $list1,
                'list2' => $list2,
            ]);
            return $this->fetch();
        }
    }
    
    /**
    * 删除
    */
    public function del(){
        if($this->request->isAjax()){
            $id = (int)$this->request->post('id');
            $model = new AM();
            $ret = $model->_remove($id);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    /**
    * 批量删除
    */
    public function delAll(){
        if($this->request->isAjax()){
            $ids = (string)$this->request->post('ids');
            $model = new AM();
            $ret = $model->_removeAll($ids);
            if(!$ret){
                die(outputError($model->getError()));
            }
            
            die(outputSuccess([], '删除成功'));
        }else{
            die(outputError('请求方式错误'));
        }
    }
    
    

    /**
    * 设置管理状态
    */
    public function set_status(){
        $request = $this->request->post();
        //var_dump($request);die;
        $model = new AM();
        $ret = $model->_setStatus($request);
        if(!$ret){
            die(outputError($model->getError()));
        }
        
        die(outputSuccess());
    }
    
    
    /**
    * 模型入口地址
    */
    public function entrance(){
        $id = $this->request->get('id');
        if(empty($id)){
            die('请求异常');
        }
        $mode1 = new AM();
        $data = $mode1->_findOne($id);
        if(empty($data)){
            die('该栏目不存在');
        }
        $con = $data['model_alias'];
        $act = $data['entrance_action'];
        $url = url("$con/$act", 'front_id='.$id, config('url_html_suffix'), true);
        $root = $this->request->root(true);
        $url = $root."/admin/".$con."/".$act."?front_id=".$id;
        //var_dump($url);die;

        header("Location:{$url}");exit;
    }

}
